package com.wzy.Controller.admin;

import com.wzy.entity.Blog;
import com.wzy.service.BattleService;
import com.wzy.service.BlogService;
import com.wzy.service.ViolenceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping("/admin")
public class BlogsController {

    @Autowired
    private BlogService blogService;

    @Autowired
    private BattleService battleService;

    @Autowired
    private ViolenceService violenceService;

    /**
     * 博客首页
     * @param page 页码（默认为1）
     * @param condition 查询条件
     * @return 视图模型
     */
    @RequestMapping("/blog")
    public ModelAndView index(@RequestParam(value = "page",defaultValue = "1") Integer page, Blog condition){
        return new ModelAndView("admin/blog/blog")
                .addObject("pager",this.blogService.list(condition, page))
                .addObject("condition",condition);
    }

    /**
     * 跳转到新增页
     * @param model 向页面传递战役和暴行
     * @return 跳转新增页面地址
     */
    @GetMapping("/blog/append")
    public String goAppend(Model model){
        model.addAttribute("battles",this.battleService.listNotAppend());
        model.addAttribute("violences",this.violenceService.listNotAppend());
        return "admin/blog/append";
    }

    /**
     * 新增操作
     * @param blog 新增的博客
     * @param attributes 重定向页面操作信息
     * @return 重定向到首页
     */
    @PostMapping("/blog/append")
    public String append(Blog blog, RedirectAttributes attributes){
        if(this.blogService.append(blog)!=0){
            attributes.addFlashAttribute("message","新增成功！");
        }else{
            attributes.addFlashAttribute("message","新增失败！");
        }
        return "redirect:/admin/blog";
    }

    /**
     * 跳转到编辑页
     * @param model 向页面传递博客详情
     * @return 跳转编辑页面地址
     */
    @GetMapping("/blog/modify")
    public String goModify(Long blogId,Model model){
        model.addAttribute("blog",this.blogService.detail(blogId));
        return "admin/blog/modify";
    }

    /**
     * 编辑操作
     * @param blog 要编辑的博客
     * @param attributes 重定向页面操作信息
     * @return 重定向到首页
     */
    @PostMapping("/blog/modify")
    public String modify(Blog blog, RedirectAttributes attributes){
        if(this.blogService.modify(blog)!=0){
            attributes.addFlashAttribute("message","编辑成功！");
        }else{
            attributes.addFlashAttribute("message","编辑失败！");
        }
        return "redirect:/admin/blog";
    }

    /**
     * 删除博客
     * @param blogId 删除博客ID
     * @param attributes 重定向向页面传输信息
     * @return 重定向的页面
     */
    @GetMapping("/blog/delete")
    public String delete(Long blogId,RedirectAttributes attributes){
        if(this.blogService.removeById(blogId)){
            attributes.addFlashAttribute("message","删除成功！");
        }else{
            attributes.addFlashAttribute("message","删除失败！");
        }
        return "redirect:/admin/blog";
    }
}
